﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace TsInterfaceRazor.analysis
{
    public static class TypeExtend
    {
        /// <summary>
        /// 判断是否是可空类型
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public static bool IsNullableType(this Type type)
        {
            if (type.IsGenericType)
            {
                var definition = type.GetGenericTypeDefinition();
                if (definition != null && definition == typeof(Nullable<>))
                {
                    
                    return true;
                }
                else
                    return false;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 判断是否是可空类型
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public static bool IsNullableType(this PropertyInfo propertyInfo)
        {
            return propertyInfo.PropertyType.IsNullableType();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public static bool IsIEnumerableType(this Type type)
        {
            if (type.IsGenericType)
            {
                var definition = type.GetGenericTypeDefinition();
                if (definition != null && (definition == typeof(IEnumerable<>) || definition == typeof(IList<>) || definition == typeof(List<>)))
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            else
            {
                return false;
            }

        }

        public static bool IsIEnumerableType(this PropertyInfo propertyInfo)
        {
            return propertyInfo.PropertyType.IsIEnumerableType();

        }
    }
}
